/**
 * Created by gengrong on 16/4/5.
 */
import {LayerLink,Panel} from 'comps/common';
import * as React from 'react';
import {Table} from 'antd';
import {formatDateByMilliseconds} from 'utils';
import moment from 'moment';

class TracingDetailList extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      tableExpandedRowKeys: []
    };
  }

  columns = [
    {
      title: '服务名',
      dataIndex: 'interfaceName',
      key: 'interfaceName'
    }, {
      title: '方法名',
      dataIndex: 'methodName',
      key: 'methodName'
    }, {
      title: '执行时间',
      dataIndex: 'startTime',
      key: 'startTime',
      render: startTime => moment(startTime)
        .format('YYYY-MM-DD HH:mm:ss.SSSS')
    }, {
      title: '执行耗时',
      dataIndex: 'startTime',
      key: 'endTime',
      render(startTime, record){
        const runTime = record.endTime - startTime;
        return `${runTime} ms`;
      }
    }, {
      title: '执行状态',
      dataIndex: 'status',
      key: 'status',
      render(status) {
        return {
          OK: <span className="color-success">正常</span>,
          EXCEPTION: <span className="color-danger">异常</span>,
          TIME_OUT: <span className="color-warning">超时</span>
        }[status];
      }
      //}, {
      //  title: '操作',
      //  key: 'operation',
      //  render() {
      //    return '管理';
      //  }
    }
  ]

  getExpandedRowKeys(tracingDetailList) {
    const defaultExpandedRowKeys = [];

    const getRowKey = (record) => {
      if (record.children && record.children.length > 0) {
        defaultExpandedRowKeys.push(record.tracingId)
        record.children.forEach(child => {
          getRowKey(child)
        })
      }
    }

    tracingDetailList.forEach(record => {
      getRowKey(record)
    })

    return defaultExpandedRowKeys;
  }

  componentWillReceiveProps(nextProps) {
    if ('tracingDetailList' in nextProps) {
      this.setState({
        tableExpandedRowKeys: this.getExpandedRowKeys(nextProps.tracingDetailList)
      });
    }
  }

  onExpandedRowsChange(expandedRowKeys) {
    this.setState({
      tableExpandedRowKeys: expandedRowKeys
    })
  }

  render() {
    return (
      <Panel title="追踪记录详情">
        <Table dataSource={this.props.tracingDetailList} columns={this.columns} pagination={false} size="middle"
          rowKey={record => record.tracingId} expandedRowKeys={this.state.tableExpandedRowKeys}
          onExpandedRowsChange={::this.onExpandedRowsChange}/>
      </Panel>
    )
  }
}

export default TracingDetailList;
